package vg.modules.navigator.components.popup_menu_component.popup_menu_items;

import com.google.common.collect.Lists;
import vg.interfaces.ll_services.data_base_service.data.graph.Graph;
import vg.modules.navigator.components.SmartTreeComponent;
import vg.services.graph_view_manager.interfaces.graph_comparison.AGraphComparisonView;
import vg.services.main_manager.VGMainServiceHelper;
import vg.services.plugin_manager.event_and_request.request.UIRequestAddTab;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.util.Collection;
import java.util.List;

public class GraphComparisonPopupMenuItem implements IPopupMenuItem {
    // Constants
    //private static final Icon openVerticesIcon;

    // Main components
    static {
        //openVerticesIcon = new ImageIcon(FileUtils.getWorkingDir() + "data/resources/textures/navigator/vertex.png");
    }

    @Override
    public void action(Collection<JTree> trees, JTree lastSelectedTree) {
        List<TreePath> treePaths = Lists.newArrayList();
        for (JTree tree : trees) {
            TreePath[] paths = tree.getSelectionPaths();
            if (paths != null) {
                treePaths.addAll(Lists.newArrayList(paths));
            }
        }

        if (treePaths.size() != 2) {
            VGMainServiceHelper.windowMessenger.errorMessage("Please, select two graphs for comparison");
            return;
        }

        SmartTreeComponent.SmartTreeNode smartTreeNode1 = (SmartTreeComponent.SmartTreeNode)treePaths.get(0).getLastPathComponent();
        SmartTreeComponent.SmartTreeNode smartTreeNode2 = (SmartTreeComponent.SmartTreeNode)treePaths.get(1).getLastPathComponent();

        if (!smartTreeNode1.isGraphHeader() || !smartTreeNode2.isGraphHeader()) {
            VGMainServiceHelper.windowMessenger.errorMessage("Please, select two graphs for comparison");
            return;
        }

        Graph graph1 = VGMainServiceHelper.graphDataBaseManager.getGraph(smartTreeNode1.getGraphHeader().getId());
        Graph graph2 = VGMainServiceHelper.graphDataBaseManager.getGraph(smartTreeNode2.getGraphHeader().getId());
        AGraphComparisonView gc = VGMainServiceHelper.graphViewManager.newGraphComparison(graph1, graph2);
        VGMainServiceHelper.userInterfaceManager.sendRequest(new UIRequestAddTab(gc));
    }

    @Override
    public boolean isVisible(Collection<JTree> trees, JTree lastSelectedTree) {
        List<TreePath> treePaths = Lists.newArrayList();
        for (JTree tree : trees) {
            TreePath[] paths = tree.getSelectionPaths();
            if (paths != null) {
                treePaths.addAll(Lists.newArrayList(paths));
            }
        }
        if (treePaths.size() != 2) {
            return false;
        }

        SmartTreeComponent.SmartTreeNode smartTreeNode1 = (SmartTreeComponent.SmartTreeNode)treePaths.get(0).getLastPathComponent();
        SmartTreeComponent.SmartTreeNode smartTreeNode2 = (SmartTreeComponent.SmartTreeNode)treePaths.get(1).getLastPathComponent();

        return smartTreeNode1.isGraphHeader() && smartTreeNode2.isGraphHeader();
    }

    @Override
    public Icon getIcon() {
        return null;
    }

    @Override
    public String getActionName() {
        return "Compare two graphs";
    }
}

